﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace DataBaseBackUpUtil
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            txtUserName.Text = "root";
            txtPassword.Text = "111";
            txtDataBaseName.Text = "11111";
            txtMySqlInstallPath.Text = @"C:\Program Files\MySQL\MySQL Server 5.7\bin";
            txtBackUpFolderPath.Text = @"C:\BackUp\";
            txtReceiveMailAddress.Text = "111111@qq.com";
            txtHour.Text = "1";
            txtMinute.Text = "1";
            btnStopTask.Enabled = false;
        }


        private void btnBackUp_Click(object sender, EventArgs e)
        {
            string BackUpFilePath = string.Empty;
            BackUpUtil.BackUpDB(txtMySqlInstallPath.Text, txtBackUpFolderPath.Text, txtUserName.Text, txtPassword.Text, txtDataBaseName.Text, ref BackUpFilePath, (s, arg) => { });
        }

        private void btnBackUpAndSendMail_Click(object sender, EventArgs e)
        {
            string BackUpFilePath = string.Empty;
            BackUpUtil.BackUpDB(txtMySqlInstallPath.Text, txtBackUpFolderPath.Text, txtUserName.Text, txtPassword.Text, txtDataBaseName.Text, ref BackUpFilePath, (s, arg) =>
            {
                if (!string.IsNullOrEmpty(BackUpFilePath) && File.Exists(BackUpFilePath))
                {
                    try
                    {
                        DataBaseBackUpUtil.FMail.MailModel mailmodel = new DataBaseBackUpUtil.FMail.MailModel();
                        mailmodel.Content = string.Format("{0}数据库{1}备份", DateTime.Now.ToString("yyyy年MM月dd日"), txtDataBaseName.Text);
                        mailmodel.ReceiverAddress = txtReceiveMailAddress.Text;
                        mailmodel.ReceiverName = "数据库DBA";
                        mailmodel.Title = "数据库备份邮件发送";
                        mailmodel.SenderName = "系统管理员";
                        mailmodel.SenderAddress = "506511437@qq.com";
                        mailmodel.SenderPassword = "boowqsfpgjfjbjdf";
                        mailmodel.AttachFilePath = BackUpFilePath;
                        FMail.SendMail(mailmodel);
                        FLog.WriteLog("发送邮件成功");
                    }
                    catch (Exception ex)
                    {
                        FLog.WriteLog("发送邮件失败,异常信息：" + ex.Message);
                        throw;
                    }
                }
            });


        }

        private void btnBackUpAndSendMailTask_Click(object sender, EventArgs e)
        {
            Dictionary<string, string> dic = new Dictionary<string, string>();
            dic.Add("MySqlInstallPath", txtMySqlInstallPath.Text);
            dic.Add("BackUpFolderPath", txtBackUpFolderPath.Text);
            dic.Add("UserName", txtUserName.Text);
            dic.Add("Password", txtPassword.Text);
            dic.Add("DataBaseName", txtDataBaseName.Text);
            dic.Add("ReceiveMailAddress", txtReceiveMailAddress.Text);
            dic.Add("Hour", txtHour.Text);
            dic.Add("Minute", txtMinute.Text);
            TaskManage.StartTask(dic);
            btnStopTask.Enabled = true;
            btnBackUpAndSendMailTask.Enabled = false;
        }

        private void btnStopTask_Click(object sender, EventArgs e)
        {
            TaskManage.RemoveTask();
            btnStopTask.Enabled = false;
            btnBackUpAndSendMailTask.Enabled = true;
        }

        protected override void OnClosing(CancelEventArgs e)
        {
            TaskManage.StopTask();
            base.OnClosing(e);
        }

        private void notifyIcon1_MouseDoubleClick(object sender, MouseEventArgs e)
        {
            if (WindowState == FormWindowState.Minimized)
            {
                //还原窗体显示    
                WindowState = FormWindowState.Normal;
                //激活窗体并给予它焦点
                this.Activate();
                //任务栏区显示图标
                this.ShowInTaskbar = true;
                //托盘区图标隐藏
                notifyIcon1.Visible = false;
            }
        }

        private void Form1_FormClosing(object sender, FormClosingEventArgs e)
        {
            if (MessageBox.Show("是否确认退出程序？", "退出", MessageBoxButtons.OKCancel, MessageBoxIcon.Question) == DialogResult.OK)
            {
                // 关闭所有的线程
                //this.Dispose();
                //this.Close();
                System.Environment.Exit(0);
            }
            else
            {
                e.Cancel = true;
            }
        }

        private void Form1_SizeChanged(object sender, EventArgs e)
        {
            //判断是否选择的是最小化按钮
            if (WindowState == FormWindowState.Minimized)
            {
                //隐藏任务栏区图标
                this.ShowInTaskbar = false;
                //图标显示在托盘区
                notifyIcon1.Visible = true;
            }
        }
    }
}
